#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1e5 + 10;
int h[N], e[N], ne[N], idx;
int dis[N];
int n, m;
void add(int a, int b)
{
	e[idx] = b;
	ne[idx] = h[a];
	h[a] = idx++;
}
void bfs()
{
	memset(dis, -1, sizeof(dis));
	queue<int>q;
	q.push(1);
	dis[1] = 0;
	while (q.size()) {
		int t = q.front();
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (dis[j] == -1) {
				dis[j] = dis[t] + 1;
				q.push(j);
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	int a, b;
	memset(h, -1, sizeof(h));
	while (m--) {
		cin >> a >> b;
		add(a, b);
	}
	bfs();
	cout << dis[n] << endl;
	return 0;
}